<?php

class Blog_Action_Create extends Action_User
{
	function execute($request)
	{
		$form = Form::factory('blog/create');

		if ($request -> isPost() && $form -> isValid($_POST))
		{

			$data = $form -> getData();

			$item = Document::factory('blog');

			$item -> _id = $item -> collection() -> getAutoID();

			$item -> title = $data['title'];

			$item -> alias = Text::toAlias($data['title'], $item -> _id);

			$item -> content = $data['content'];

			$item -> timestamp = time();

			$item -> user_id = Agilet::getViewer() -> _id;

			$item -> created_ip = $request -> getClientIp();

			$item -> created_at = time();

			$item -> save(true);
			
			if ($hooks = Hook::load($act = 'blogCreated'))
			{
				Hook::emit($hooks, $act, $item);
			}
		}

		$request -> response = View::render($script = 'blog/html/create', array('form' => $form));
	}

}
